<template>
  <a-modal :open="open" width="800px" title="修改登记信息" centered @ok="onOk" @cancel="onCancel">
    <div class="bg-secondary w-full px-4 py-2 rounded-md border border-solid border-primary">
      <div class="flex items-end">
        <div class="flex items-end w-[240px]">
          <div class="text-lg font-semibold text-primary max-w-[120px] truncate">{{ formState?.brName }}</div>
          <div class="mx-3">{{ formState?.brXb }}</div>
          <div>{{ formState?.brAge || '-' }}</div>
        </div>
        <LabelValue :width="240" label="门诊号" :value="formState.jzbh" />
        <LabelValue :width="240" label="操作员" :value="formState.czymc" />
      </div>
      <div class="flex items-center mt-2">
        <LabelValue label="医生" :width="240" :value="formState.empName" />
        <LabelValue label="科室" :width="240" :value="formState.ksmc" />
        <LabelValue label="挂号时间" :width="240" :value="formState.ghsj" />
      </div>
    </div>
    <a-form :model="formState" class="form-style-flex mt-4">
      <a-form-item label="就诊事项" class="w-1/2">
        <a-select v-model:value="formState.dictOutpMatterId" placeholder="请选择就诊事项" allow-clear>
          <a-select-option v-for="(item, index) in dictOutList" :key="index" :value="item.dictOutpMatterId">{{ item.matterName }}</a-select-option>
        </a-select>
      </a-form-item>
      <a-form-item label="关注项目" class="w-1/2">
        <SelectFocusProject v-model:value="formState.brZlxm" :max-tag-count="1" placeholder="请选择" />
      </a-form-item>
      <a-form-item label="客户来源" class="w-1/2">
        <SelectCustomerSource v-model:value="formState.brly" />
      </a-form-item>
      <a-form-item label="介绍人" class="w-1/2">
        <!-- <SelectTable
          v-model:value="formState.refereeEmpId"
          custom-input
          type="员工"
          page-size="999"
          @clear="formState.refereeEmpId = ''"
          @select="
            (select) => {
              formState.refereeEmpId = select?.empName
            }
          "
        /> -->
        <a-input v-model:value="formState.refereeEmpId" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="业务员" class="w-1/2">
        <SelectTable
          v-model:value="formState.brYwyName"
          type="员工"
          page-size="999"
          @clear="formState.brYwy = ''"
          @select="onSelectEmployee('brYwy', $event)"
        />
      </a-form-item>
      <a-form-item label="负责人" class="w-1/2">
        <SelectTable
          v-model:value="formState.brFzrName"
          :disabled="formState.isCode"
          type="员工"
          page-size="999"
          @clear="formState.brFzr = ''"
          @select="onSelectEmployee('brFzr', $event)"
        />
      </a-form-item>
      <a-form-item label="咨询师" class="w-1/2">
        <SelectTable
          v-model:value="formState.brZxsName"
          :disabled="formState.isCode"
          type="员工"
          page-size="999"
          :search-params="{ emplbid: '14' }"
          @select="onSelectEmployee('brZxs', $event)"
          @clear="formState.brZxs = ''"
        />
      </a-form-item>
    </a-form>
  </a-modal>
</template>
<script setup>
import { nextTick, ref, watch } from 'vue'
import { message } from 'ant-design-vue'
import { post } from '@/utils/request'

const emit = defineEmits(['update:open', 'update:value', 'success'])
const props = defineProps({
  open: Boolean,
  rowData: {
    type: Object,
    default: () => ({})
  }
})
const formState = ref({})
const dictOutList = ref([])

// 获取就诊事项
const getDictOutList = async () => {
  const res = await post('/outp/mzdj/getDictOutpMatterNumberList')
  if (res.code !== 0) return message.error(res.msg)
  dictOutList.value = res.data
}

const onSelectEmployee = (key, select) => {
  formState.value[key + 'Name'] = select.empName
  formState.value[key] = select.empId
}

const onOk = async () => {
  const response = await post('/outp/mzdj/modificationRegistration', {
    jzbh: formState.value.jzbh,
    brId: formState.value.brId,
    dictOutpMatterId: formState.value.dictOutpMatterId
  })
  if (response.code !== 0) return message.error(response.msg)
  const res = await post('/common/patient/doSavePatientInfoEmp', {
    brId: formState.value.brId,
    brName: formState.value.brName,
    brZlxm: formState.value.brZlxm,
    brly: formState.value.brly,
    refereeEmpId: formState.value.refereeEmpId,
    brYwy: formState.value.brYwy,
    brFzr: formState.value.brFzr,
    brZxs: formState.value.brZxs
  })
  if (res.code === 0) {
    message.success('保存成功')
    emit('success')
  } else {
    message.error(res.msg || '保存失败')
  }
}

const onCancel = () => {
  emit('update:open', false)
}

watch(
  () => props.open,
  async () => {
    await getDictOutList()
    nextTick(() => {
      formState.value = { ...props.rowData }
      formState.value.brAge = String(formState.value.brAge)
    })
  }
)
</script>
<style lang="less" scoped></style>
